<template>
  <div class="app-container">
    <div class="filter-container">
      <el-input v-model="listQuery.courseName" placeholder="课程名称" style="width: 200px;" class="filter-item" @keyup.enter.native="handleFilter" />
      <el-button v-waves class="filter-item" type="primary" icon="el-icon-search" @click="handleFilter">
        查询
      </el-button>
    </div>

    <el-table
      :key="tableKey"
      v-loading="listLoading"
      :header-cell-style="{background:'#ccc',color:'black'}"
      :data="list"
      border
      stripe
      fit
      highlight-current-row
      style="width: 100%;"
    >
      <el-table-column label="ID" align="center" width="40">
        <template slot-scope="{row}">
          <span>{{ row.id }}</span>
        </template>
      </el-table-column>
      <el-table-column label="课程名称" align="center">
        <template slot-scope="{row}">
          <span>{{ row.courseInfo.name }}</span>
        </template>
      </el-table-column>
      <el-table-column label="课程简称" align="center">
        <template slot-scope="{row}">
          <span>{{ row.courseInfo.short_name }}</span>
        </template>
      </el-table-column>
      <el-table-column label="上课开始时间" prop="semester" align="center">
        <template slot-scope="{row}">
          <span>{{ row.courseTimetableInfo.stime }}</span>
        </template>
      </el-table-column>
      <el-table-column label="上课结束时间" prop="semester" align="center">
        <template slot-scope="{row}">
          <span>{{ row.courseTimetableInfo.etime }}</span>
        </template>
      </el-table-column>
      <el-table-column label="考勤" prop="semester" align="center">
        <template slot-scope="{row}">
          <span>{{ kaoqinIsPassOptions[row.kaoqin.is_pass] }}</span>
        </template>
      </el-table-column>
      <el-table-column label="作业方式" align="center">
        <template slot-scope="{row}">
          <span>{{ homeworkTypeOptions[row.type] }}</span>
        </template>
      </el-table-column>
      <el-table-column label="作业状态" align="center">
        <template slot-scope="{row}">
          <span v-if="JSON.stringify(row.commit) === '[]'">未提交</span>
          <span v-else>{{ commitStatusOption[row.commit.commit_status] }}<a :href="returnImageUrl(row.commit.url)" target="_blank" rel="noopener">:预览</a></span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" width="230" class-name="small-padding fixed-width">
        <template slot-scope="{row}">
          <el-button v-if="(row.etime > row.systemTime || ( row.btime !== null && row.btime > row.systemTime )) && (row.type == 1 || row.courseTimetableUserInfo.is_group_manage == 1)" type="primary" size="mini" @click="handleUpdate(row)">
            提交作业
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-dialog :title="textMap[dialogStatus]" :visible.sync="dialogFormVisible">
      <el-form ref="dataForm" :rules="rules" :model="temp" label-position="left" label-width="110px" style="width: 400px; margin-left:50px;">
        <el-form-item label="作业" prop="url">
          <PdfUpload :upload-name="uploadName" :upload-url="temp.url" @getReturnUrl="setReturnUrl" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">
          取消
        </el-button>
        <el-button type="primary" @click="dialogStatus==='create'?createData():updateData()">
          确定
        </el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { studentHomeworkCommitPage, homeworkCommit } from '@/api/homework'

import waves from '@/directive/waves' // waves directive
import { notifySuccess, listQueryFilter, getFileUrl } from '@/utils/tool'
import PdfUpload from '@/components/Upload/PdfUpload'

export default {
  name: 'HomeworkStudent',
  components: { PdfUpload },
  directives: { waves },
  data() {
    return {
      tableKey: 0,
      list: null,
      total: 0,
      listLoading: true,
      listQuery: {
        courseName: undefined
      },
      kaoqinIsPassOptions: { 0: '不合格', 1: '合格' },
      homeworkTypeOptions: { 1: '个人作业', 2: '班级小组作业' },
      commitStatusOption: { 0: '未提交', 1: '已提交', 2: '已打回' },
      temp: {
        url: ''
      },
      dialogFormVisible: false,
      dialogStatus: '',
      textMap: {
        update: '编辑',
        create: '添加'
      },
      rules: {
        url: [{ required: true, message: '作业地址必填', trigger: 'change' }]
      },
      uploadName: 'UpLoadPdf',
      downloadLoading: false
    }
  },
  created() {
    this.getList()
  },
  methods: {
    getList() {
      this.listLoading = true
      this.listQuery = listQueryFilter(this.listQuery)
      studentHomeworkCommitPage(this.listQuery).then(response => {
        this.list = response.data
        this.total = response.page
        this.listLoading = false
      })
    },
    handleFilter() {
      this.listQuery.page = 1
      this.getList()
    },
    resetTemp() {
      this.temp = {
        homeworkId: '',
        url: '',
        userId: ''
      }
    },
    handleUpdate(row) {
      this.temp.homeworkId = row.id
      this.temp.url = row.url
      this.dialogStatus = 'update'
      this.dialogFormVisible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },
    updateData() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          const tempData = Object.assign({}, this.temp)
          homeworkCommit(tempData).then(() => {
            this.dialogFormVisible = false
            notifySuccess(this, '修改成功')
            this.getList()
          })
        }
      })
    },
    setReturnUrl: function(data) {
      this.temp.url = data
      notifySuccess(this, 'pdf文件上传成功,请点击确定修改')
    },
    returnImageUrl(url) {
      return getFileUrl(url)
    }
  }
}
</script>

